<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/11/6
 * Time: 20:31
 */

namespace App\Services\Api;
use App\admin\Service\OA;
use App\Jobs\Fun;
use App\Models\Admin\YzmModel;
use App\Models\LogModel;
use App\Models\TokenUrlModel;
use App\Models\UploadPicModel;
use App\Services\Search\SearchService;
use App\Utilities\Utility;
use Illuminate\Http\Request;

/// App\Services\Api\SMS@
class SMS extends ApiBase
{

    /**
     * 向用户发送验证码
    */
    public function huoquyanzhengma(Request $request){
        /* $sms = new \App\Utilities\Sms();
        $msg = "【美策】验证码 ，您正在进行美策平台的密码修改，有效时间30分钟身份验证，打死不要告诉别人哦！";

        $resSms = $sms->sendSMS('15560195082', $msg);
        print_r($resSms);
        exit;


*/




        $user=OA::$user->getModel();
        $phone=$user['phone'];
        $exists=YzmModel::whereRaw('phone = ? AND created_at > ?',[
            $phone,time()-(60*5)
        ])->exists();
        if($exists){
            OA::Json(1,'刚刚已经发送过了,短期内不能多次发送短信');
        }

        $code = Utility::getRandStr(4, 0);
        //print_r($code);exit;
        /// YzmModel::deleted();
        $dataArr = array(
            'phone' => $phone,
            'code' => $code,
            'created_at' => time(),
        );
        $lastId = YzmModel::insertGetId($dataArr);
        $sms = new \App\Utilities\Sms();
        $msg = "【美策】验证码{$code}，您正在进行美策平台的密码修改，有效时间5分钟身份验证，打死不要告诉别人哦！";

        $resSms = $sms->sendSMS($phone, $msg);
        $resSms = json_decode($resSms, true);
        //print_r($resSms);exit;
        if (empty($resSms) || !empty($resSms['errorMsg'])) {
            LogModel::add("短信发送失败,".json_encode($resSms),LogModel::typeError);
            OA::Json(1,'系统错误,请稍候');
        }
        YzmModel::where('id', $lastId)->update(array('status' => 2));
        OA::Json(0);
    }


}